//import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/material.dart';
//import 'package:crypto/crypto.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// widget扩展类
extension WidgetExtension on Widget {
  // 给widget增加expanded包裹
  Widget get expanded {
    return Expanded(
      child: this,
    );
  }

  // 当条件为false的时候展示widget
  Widget hidden(bool hiddenWidget) {
    return hiddenWidget ? Container() : this;
  }

  // 切圆角 radius默认8
  Widget radius({double radius = 8, Clip clipBehavior = Clip.antiAlias}) {
    return Container(
      clipBehavior: clipBehavior,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(radius.r)),
      ),
      child: this,
    );
  }

  // 加边框与圆角 radius默认8
  Widget border(
      {Color borderColor = const Color(0xffDEDEDE), double radius = 9}) {
    return Container(
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        border: Border.all(color: borderColor, width: 1),
        borderRadius: BorderRadius.all(Radius.circular(radius)),
      ),
      child: this,
    );
  }

  // 加阴影 模糊半径默认为10
  Widget shadow(
      {Color shadowColor = const Color.fromARGB(137, 161, 153, 153),
      double blurRadius = 10}) {
    return Container(
      decoration: BoxDecoration(boxShadow: [
        BoxShadow(
          color: shadowColor,
          blurRadius: blurRadius, // 模糊半径为10
        ),
      ]),
      child: this,
    );
  }

  // 为widget增加一个onClick
  Widget onClick(callback,{bool isForbiddenTap=false}) {
    return InkWell(
      onTap: (){
        if(isForbiddenTap){

        }else{
          callback();
        }
      },
      child: this,
    );
  }

  // 是否开启用户交互
  Widget userInterface(bool interface) {
    if (interface) {
      return this;
    }
    return IgnorePointer(
      child: this,
    );
  }
}

// num  方法扩展
extension NumExtension on num {
  // 获取一个横向间距 sizedBox
  Widget get sizedBoxWidth {
    return SizedBox(width: toDouble().w);
  }

  // 获取一个纵向间距 sizedBox
  Widget get sizedBoxHeight {
    return SizedBox(height: toDouble().h);
  }

  // 获取一个textsize 为自动适配的 TextStyle
  TextStyle get ts {
    return TextStyle(
        overflow: TextOverflow.ellipsis,
        fontSize: toDouble().sp,
        fontWeight: FontWeight.w400,
        color: Colors.white);
  }

  // auto resize  自动根据屏幕宽高适配
  // double get a {
  //   return Utils().getWidth(toDouble());
  // }

  // auto resize sp 自动根据屏幕宽高适配
  // double get as {
  //   return Utils().getSp(toDouble());
  // }
}

extension ContextExtension on BuildContext {
  double get width {
    return MediaQuery.of(this).size.width;
  }

  double get height {
    return MediaQuery.of(this).size.height;
  }

  /// 获取当前组件的 RenderBox
  RenderBox? renderBox() {
    return findRenderObject() is RenderBox
        ? (findRenderObject() as RenderBox)
        : null;
  }

  /// 获取当前组件的 position
  Offset? position({Offset offset = Offset.zero}) {
    return renderBox()?.localToGlobal(offset);
  }
}

extension StringExtension on String {
  /// 检查邮箱格式
  bool get isEmail {
    if (isEmpty) return false;
    // 邮箱正则
    String regexEmail = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$";
    return RegExp(regexEmail).hasMatch(this);
  }

  /// 检查手机格式
  bool get isMobile {
    if (isEmpty) return false;
    // 手机正则
    String regexEmail =
        "^((13[0-9])|(15[^4])|(16[5,6])|(17[0-8])|(18[0-9])|(19[1,8-9])|(14[4,5,7])|(156))\\d{8}\$";
    return RegExp(regexEmail).hasMatch(this);
  }

  // MD5加密
  // String get md5String {
  //   // md5 加密
  //   return md5.convert(utf8.encode(this)).toString();
  // }

  // hex加密
  String get hexString {
    if (isEmpty) throw ArgumentError("hex is null");

    var result = Uint8List(length ~/ 2);
    for (var i = 0; i < length; i += 2) {
      var num = substring(i, i + 2);
      var byte = int.parse(num, radix: 16);
      result[i ~/ 2] = byte;
    }
    return result.toString();
  }
  //生成8位的字符串
  String generateRandomString(int length) {
    const String charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = Random();
    StringBuffer buffer = StringBuffer();

    for (int i = 0; i < length; i++) {
      int randomIndex = random.nextInt(charset.length);
      buffer.write(charset[randomIndex]);
    }

    return buffer.toString();
  }

}

extension GlobalKeyExt on GlobalKey {
  /// 获取当前组件的 RenderBox
  RenderBox? renderBox() => currentContext?.renderBox();

  /// 获取当前组件的 position
  Offset? position({Offset offset = Offset.zero}) =>
      currentContext?.position(offset: offset);

  /// 获取当前组件的 Size
  Size? get size => currentContext?.size;
}
